package com.open.capacity.reptile.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.reptile.entity.GdszczPayVoucherBill;
import com.open.capacity.reptile.excel.PayVoucherBillExcelModel;
import com.open.capacity.reptile.service.GdszczMaEleAccoService;
import com.open.capacity.reptile.service.GdszczPayVoucherBillService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("payvoucherbills")
@Api(tags = "支付凭证")
public class GdszczPayVoucherBillController {

    @Autowired
    private GdszczPayVoucherBillService payVoucherBillService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult result = payVoucherBillService.pageAll(params);
        return result;
    }


    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('pz:pz:list')")
    public Result findById(Long id) {
        return Result.succeed(payVoucherBillService.findById(id));
    }


    /**
     * 导出数据
     */
    @GetMapping("/exportExcel")
    @PreAuthorize("hasAuthority('base_xm:basexm:export')")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        List<GdszczPayVoucherBill> list = payVoucherBillService.findAll(params);

        List<PayVoucherBillExcelModel> result = list.stream().map(item -> {
            PayVoucherBillExcelModel model = new PayVoucherBillExcelModel();
            BeanUtils.copyProperties(item, model);
            return model;
        }).collect(Collectors.toList());

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=payvoucherbill.xls");
        OutputStream ouputStream = null;
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
                PayVoucherBillExcelModel.class, result);
        try {
            ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
        } catch (Exception e) {
            throw new RuntimeException("系统异常");
        } finally {
            try {
                if (ouputStream != null) {
                    ouputStream.flush();
                    ouputStream.close();
                }
            } catch (Exception e) {
                throw new RuntimeException("系统异常");
            }
        }
    }
}
